

*Settings
clear all
cap log close
*Change this to your system name
local user "awcassidy1"
set more off, permanently
cd "C:\Users\awcassidy1\Dropbox\jmp_new\cleaned_data"
set scheme plotplainblind



use "C:\Users\awcassidy1\Dropbox\jmp_new\cleaned_data/data_with_observability_indices.dta", clear


********************************************************************************
*Baseline: observability groups
********************************************************************************
*standard values
local clustvar "mlsid"
local clustvarword "Home"
local fevars "i.saleyear i.lsaleyear i.yearbuiltcat"
local fe_to_absorb="saleyear lsaleyear yearbuiltcat"
local controls "yearbuiltaudit conditionedsqft"
local baselinevars "parsimonious_less parsimonious_more_fuel parsimonious_more_other"
local filename "main"
local depvar "pricediff"
local not_extracted="duct_type  rsavg  eeravg  sizeavg    negsysageavg ah_type notwinrec  notraddr"
*sample criterion is a full if statement

local parsimonious_less_vars "negleakavg atticrvalue ductravg     "
local parsimonious_more_vars_fuel "fftype whftype "
local parsimonious_more_vars_other "ah_location progtherm twosystems"


********************************************************************************
*Exploring Above Median Less using matching
********************************************************************************
*note balance was worse when using wh_tank type as a variable to match on, so 
*we omitted. Balance also worse when using just More_Fuel, just More_Other, 
*or just EE_all_mean 

cap drop _*
cap drop match
cap drop res
cap drop support
cap drop dup

*get residuals
reghdfe pricediff   ///
	, ///
	 absorb(yearbuiltcat saleyear lsaleyear) resid
predict res, res

local match_list = "More_Other More_Fuel `not_extracted'"	
*local match_list= "EE_all_mean"	

*not residualized: show this?
eststo clear
*tebalance density

probit above_median_less `match_list'
predict propensity_score, pr

sum propensity_score
*sum viol_o

********************
*specify cutoffs for propensity score

local lb=.15
local ub=.85

********************************************************************************
*Table 6
*(has three parts stacked together in tex)
********************************************************************************
********************************************************************************
*ATET first
********************************************************************************
eststo clear
eststo: teffects psmatch (pricediff) (above_median_less `match_list', probit) , nn(1) vce(robust) ate 
estadd local description "\multicolumn{1}{c}{None}"
estadd local partialled_out_fe "\multicolumn{1}{c}{No}"

eststo: teffects psmatch (pricediff) (above_median_less `match_list', probit) if propensity_score>`lb' & propensity_score<`ub', nn(1) vce(robust) ate 
estadd local description "\in [`lb',`ub']"
estadd local partialled_out_fe "\multicolumn{1}{c}{No}"

eststo: teffects psmatch (res) (above_median_less `match_list', probit) , nn(1) vce(robust) ate 
estadd local description "\multicolumn{1}{c}{None}"
estadd local partialled_out_fe "\multicolumn{1}{c}{Yes}"

eststo: teffects psmatch (res) (above_median_less `match_list', probit) if propensity_score>`lb' & propensity_score<`ub', nn(1) vce(robust) ate
estadd local description "\in [`lb',`ub']"
estadd local partialled_out_fe "\multicolumn{1}{c}{Yes}"


esttab using "C:\Users\awcassidy1\Dropbox\jmp_new/tables/top_of_match_table.tex", replace fragment ///
		 label booktabs nomtitles noobs se(4) b(4) ///
		nodepvars ///
		 varlabels(`e(eqlabels)') ///
		 eqlabels(none) ///
		 mlabels(`e(labels)') ///
		 coeflabels(r1vs0.above_median_less "ATE") ///
		 star(* 0.10 ** 0.05 *** 0.01)
		 
eststo clear
eststo: teffects psmatch (pricediff) (above_median_less `match_list', probit) , nn(1) vce(robust) atet
estadd local description "\multicolumn{1}{c}{None}"
estadd local partialled_out_fe "\multicolumn{1}{c}{No}"

eststo: teffects psmatch (pricediff) (above_median_less `match_list', probit) if propensity_score>`lb' & propensity_score<`ub', nn(1) vce(robust) atet 
estadd local description "\in [`lb',`ub']"
estadd local partialled_out_fe "\multicolumn{1}{c}{No}"

eststo: teffects psmatch (res) (above_median_less `match_list', probit) , nn(1) vce(robust) atet 
estadd local description "\multicolumn{1}{c}{None}"
estadd local partialled_out_fe "\multicolumn{1}{c}{Yes}"

eststo: teffects psmatch (res) (above_median_less `match_list', probit) if propensity_score>`lb' & propensity_score<`ub', nn(1) vce(robust) atet
estadd local description "\in [`lb',`ub']"
estadd local partialled_out_fe "\multicolumn{1}{c}{Yes}"

esttab using "C:\Users\awcassidy1\Dropbox\jmp_new/tables/bottom_of_match_table.tex", replace fragment ///
		  booktabs nomtitles noobs se(4) b(4) ///
		 nonumbers ///
		 nodepvars ///
		 varlabels(`e(eqlabels)') ///
		 eqlabels(none) ///
		 mlabels(`e(labels)') ///
		 coeflabels(r1vs0.above_median_less "ATET") ///
		 scalars( ///
		 			"partialled_out_fe Partialled out FE?" ///
					 "description P-score Restriction" ///
					 "N Observations"  ///
				) ///
				sfmt( %9.0fc) star(* 0.10 ** 0.05 *** 0.01)
		 
		 

*********************************************************************************
*get balance table and summary statistics (Table A.2)
*********************************************************************************
eststo clear
eststo clear
teffects psmatch (res) (above_median_less `match_list' , probit) if propensity_score>`lb' & propensity_score<`ub', nn(1) vce(robust) atet
eststo: tebalance summarize `match_list' EE_all_mean the_rest_total wh_tanktype closeprice1 yearbuiltaudit conditionedsqft 
mat R=r(table)
estadd matrix R
return list
esttab e(R, fmt(%9.3fc)) using "C:\Users\awcassidy1\Dropbox\jmp_new/tables/bal_table.tex", ///
	label nodepvar replace coeflabels(`e(labels)' ) nomtitles ///
	collabels(none) ///
	b(%9.3fc) ///
	refcat(More_Other "\addlinespace \textbf{EE Vars Matched On:}" EE_all_mean  " \addlinespace \textbf{EE Vars Not Matched On:}" closeprice1 " \addlinespace \textbf{Other Vars:}", nolabel) ///
	mlabels(none) eqlabels(none) booktabs fragment
	
	
*********************************************************************************
*get density (Fig A.8)
*********************************************************************************
eststo: teffects psmatch (res) (above_median_less `match_list', probit) if propensity_score>`lb' & propensity_score<`ub', nn(1) vce(robust) ate
tebalance density, legend(label(1 "Above Median Less") label(2 "Below Median Less")) caption("")
graph export "C:\Users\awcassidy1\Dropbox\jmp_new/figs/prop_score_density.pdf", as(pdf) replace
*coeflabels(EE_all_mean "EE" closeprice1 "Price at Sale 1" )


